using NUnit.Framework;

namespace Chopping
{
   [TestFixture]
   public class ChopSpecs
   {
      [Test]
      public void Test()
      {
         Assert.AreEqual(-1, chop(3, new int[0]));
         Assert.AreEqual(-1, chop(3, new[] {1}));
         Assert.AreEqual(0, chop(1, new[] {1}));

         Assert.AreEqual(0, chop(1, new[] {1, 3, 5}));
         Assert.AreEqual(1, chop(3, new[] {1, 3, 5}));
         Assert.AreEqual(2, chop(5, new[] {1, 3, 5}));
         Assert.AreEqual(-1, chop(0, new[] {1, 3, 5}));
         Assert.AreEqual(-1, chop(2, new[] {1, 3, 5}));
         Assert.AreEqual(-1, chop(4, new[] {1, 3, 5}));
         Assert.AreEqual(-1, chop(6, new[] {1, 3, 5}));

         Assert.AreEqual(0, chop(1, new[] {1, 3, 5, 7}));
         Assert.AreEqual(1, chop(3, new[] {1, 3, 5, 7}));
         Assert.AreEqual(2, chop(5, new[] {1, 3, 5, 7}));
         Assert.AreEqual(3, chop(7, new[] {1, 3, 5, 7}));
         Assert.AreEqual(-1, chop(0, new[] {1, 3, 5, 7}));
         Assert.AreEqual(-1, chop(2, new[] {1, 3, 5, 7}));
         Assert.AreEqual(-1, chop(4, new[] {1, 3, 5, 7}));
         Assert.AreEqual(-1, chop(6, new[] {1, 3, 5, 7}));
         Assert.AreEqual(-1, chop(8, new[] {1, 3, 5, 7}));
      }

      private int chop(int numberToFind, int[] ints)
      {
         return chop(numberToFind, ints, 0, ints.Length - 1);
      }

      private int chop(int numberToFind, int[] ints, int left, int right)
      {
         if (left > right) return -1;
         int mid = (left + right) / 2;

         if (ints[mid] == numberToFind) return mid;

         return ints[mid] < numberToFind ?
            chop(numberToFind, ints, mid + 1, right) :
            chop(numberToFind, ints, left, mid - 1);
      }

      private int TheVariablesToKeepTrackOfApproach(int[] ints, int numberToFind)
      {
         var left = 0;
         var right = ints.Length - 1;

         while (true)
         {
            if (left > right) return -1;
            int mid = (left + right)/2;

            if (ints[mid] == numberToFind) return mid;
            if (ints[mid] < numberToFind) left = mid + 1;
            else right = mid - 1;
         }
      }

      private int TheIterativeApproach(int[] ints, int numberToFind)
      {
         for (int i = 0; i < ints.Length; i++)
            if (ints[i] == numberToFind)
               return i;

         return -1;
      }
   }
}